package de.slowlog;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class SlowLogApp extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		SlowLogReader reader = new SlowLogReader();
		SlowLog slowLog = reader.read("mysql-slow.log");
		showChart(stage, slowLog);
	}

	private void showFromFile(Stage stage) throws IOException {
		stage.setTitle("FXML TableView Example");
		AnchorPane pane = FXMLLoader.load(getClass().getResource(
				"AppLayout.fxml"));
		Scene scene = new Scene(pane);
		stage.setScene(scene);
		stage.show();
	}

	private void showTable(Stage stage) {
		TableView table = new TableView();

		Scene scene = new Scene(new Group());
		stage.setTitle("Table View Sample");
		stage.setWidth(500);
		stage.setHeight(500);

		final Label label = new Label("Queries");
		label.setFont(new Font("Arial", 20));

		table.setEditable(true);

		TableColumn timeCol = new TableColumn("Time");
		timeCol.setCellValueFactory(new PropertyValueFactory<>("time"));
		TableColumn durCol = new TableColumn("Duration");
		durCol.setCellValueFactory(new PropertyValueFactory<>("queryTime"));
		TableColumn queryCol = new TableColumn("Query");
		queryCol.setCellValueFactory(new PropertyValueFactory<>("query"));

		table.getColumns().addAll(timeCol, durCol, queryCol);
		ObservableList<SlowLogEntry> entries = FXCollections
				.observableArrayList(new SlowLogFactory().create().getEntries());
		table.setItems(entries);

		final VBox vbox = new VBox();
		vbox.setSpacing(5);
		vbox.setPadding(new Insets(10, 0, 0, 10));
		vbox.getChildren().addAll(label, table);

		((Group) scene.getRoot()).getChildren().addAll(vbox);

		stage.setScene(scene);
		stage.show();

	}

	private void showChart(Stage stage, SlowLog slowLog) {
		stage.setTitle("Line Chart Sample");
		// defining the axes
		final NumberAxis xAxis = new NumberAxis();
		final NumberAxis yAxis = new NumberAxis();
		xAxis.setLabel("Time");
		yAxis.setLabel("Time in seconds");
		// creating the chart
		final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(
				xAxis, yAxis);

		lineChart.setTitle("Stock Monitoring, 2010");
		// defining a series
		XYChart.Series series = new XYChart.Series();
		series.setName("Queries");

		SimpleDateFormat format = new SimpleDateFormat();
		for (SlowLogEntry entry : slowLog.getEntries()) {
			series.getData().add(
					new XYChart.Data(entry.getTime().getTime(), entry
							.getQueryTime()));
		}

		xAxis.setAutoRanging(true);
		xAxis.setForceZeroInRange(false);
		xAxis.setTickLabelFormatter(new StringConverter<Number>() {

			private DateFormat format = new SimpleDateFormat();

			@Override
			public String toString(Number object) {
				return format.format(new Date(object.longValue()));
			}

			@Override
			public Number fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		});

		Scene scene = new Scene(lineChart, 800, 600);
		lineChart.getData().add(series);

		stage.setScene(scene);
		stage.show();

	}

	public static void main(String[] args) {
		launch(args);
	}

}
